package com.mobi.Survey.lib.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mobi.Survey.lib.model.Address;
import com.mobi.Survey.lib.model.LatLong;
import com.mobi.Survey.lib.model.Pojo;
import com.mobi.Survey.lib.model.SurveyPojo;

public class AddressTable extends AbstractTable {

	public static final String TABLE = "address";

	public static final String COL_ID = "_id";
	public static final String COL_SURVEY_ID = "surveyId";
	public static final String COL_STREET_NUMBER = "StreetNumber";
	public static final String COL_STREET_NAME = "StreetName";
	public static final String COL_CITY = "City";
	public static final String COL_STATE = "State";
	public static final String COL_COUNTRY = "Country";
	public static final String COL_ZIP = "Zip";
	public static final String COL_LAT = "lat";
	public static final String COL_LONG = "long";

	public AddressTable(Context ctx) {
		super(ctx);
	}

	public AddressTable() {

	}

	@Override
	public long insertRow(Pojo obj) {

		SurveyPojo survey = (SurveyPojo) obj;
		Address address = survey.getAddress();
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		ContentValues cv = new ContentValues();
		cv.put(COL_SURVEY_ID, survey.getId());
		cv.put(COL_STREET_NUMBER, address.getStreetNumber());
		cv.put(COL_STREET_NAME, address.getStreetName());
		cv.put(COL_CITY, address.getCity());
		cv.put(COL_STATE, address.getState());
		cv.put(COL_COUNTRY, address.getCountry());
		cv.put(COL_ZIP, address.getZip());
		LatLong latLong = address.getCoordinates();
		if (null != latLong) {
			cv.put(COL_LONG, latLong.getLongitude());
			cv.put(COL_LAT, latLong.getLatitude());
		}

		long id = db.insert(TABLE, null, cv);
		return id;
	}

	@Override
	public boolean deleteRow(long id) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int effected = db.delete(TABLE, COL_SURVEY_ID + " = ?",
				new String[] { id + "" });
		return effected > 0;

	}

	public Address getAddressBySurveyId(long surveyId) {

		SQLiteDatabase db = dbHelper.getReadableDatabase();

		Cursor cursor = db.query(TABLE, null, COL_SURVEY_ID + " = ? ",
				new String[] { surveyId + "" }, null, null, null);
		Address address = new Address();
		if (cursor != null) {

			if (cursor.moveToFirst()) {

				address.setStreetNumber(cursor.getString(2));
				address.setStreetName(cursor.getString(3));
				address.setCity(cursor.getString(4));
				address.setState(cursor.getString(5));
				address.setCountry(cursor.getString(6));
				address.setZip(cursor.getString(7));

				try {
					double lat = cursor.getDouble(8);
					double lon = cursor.getDouble(9);

					address.setCoordinates(new LatLong(lon, lat));
				} catch (Exception e) {

				}

			}

			cursor.close();
			cursor = null;
		}

		return address;

	}

	@Override
	public boolean createTable(SQLiteDatabase sqliteDb) {
		String query = "create table " + TABLE + "( " + COL_ID
				+ " integer primary key autoincrement, " + COL_SURVEY_ID
				+ " BIGINT unique,  " + COL_STREET_NUMBER + " text, "
				+ COL_STREET_NAME + " text, " + COL_CITY + " text, "
				+ COL_STATE + " text, " + COL_COUNTRY + " text, " + COL_ZIP
				+ " text, " + COL_LAT + " text default null, " + COL_LONG
				+ " text default null )";
		sqliteDb.execSQL(query);
		printMessage(query);
		return true;
	}

	@Override
	public void deleteAllData() {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(TABLE, null, null);

	}

}
